// pages/Edit_equipment/Edit_equipment.js
import areaList from '../../utils/area.js'
import api from "../../Sendingmethod/Sendingmethod"
import util from '../../utils/util'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //控制弹窗
        showon: false,
        //省市信息,
        Areaarr: {},
        aername: '',
        emid: "",
        obj: {
            address: '',
            name: '',
        },
        fromdata: {
            device_id: '',
            province: '',
            city: '',
            district: '',
        },
        dpc: "",
        zhanshi: false
    },
    /**
     * 
     * 非空判断
     */
    feikomng() {
        function countEmptyValues(obj) {
            let count = 0;
            for (const key in obj) {
                if (obj.hasOwnProperty(key) && obj[key] === '') {
                    count++;
                }
            }
            return count;
        }
        let num = countEmptyValues(this.data.obj)
        console.log(num);
        if (num > 0) {
            console.log('未填写');
            this.setData({
                zhanshi: false
            })
        } else {
            console.log('放行');
            this.setData({
                zhanshi: true
            })
        }
    },

    /**
     * //省市信息 选择完成
     */
    confirmon(e) {
        console.log(e.detail.values);
        // e.detail.values
        let arr = e.detail.values
        let namearr = []
        namearr = arr.map((item, index) => item.name)
        console.log(namearr, '当前地址');
        let name = namearr.join('/')
        console.log(name, '当前地址');
        // let data1 = 'obj.address'
        this.setData({
            dpc: name,
            ['fromdata.province']: namearr[0],
            ['fromdata.city']: namearr[1],
            ['fromdata.district']: namearr[2],
            showon: false
        })

    },
    opecm() {
        this.setData({
            showon: true
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options, '传值');
        this.setData({
            emid: options.emid
        })
        console.log(areaList);
        this.setData({
            Areaarr: areaList
        })
        this.feikomng()
        console.log(this.data.Areaarr, '赋值后的对象');
    },
    input20(e) {
        console.log(e);
        let data1 = 'obj.name'
        let data2 = 'obj.address'
        if (e.target.dataset.index == 1) {
            this.setData({
                [data1]: e.detail.value
            })
            console.log(this.data.obj);
        } else {
            this.setData({
                [data2]: e.detail.value
            })
            console.log(this.data.obj);
        }
        this.feikomng()

    },
    /**
     * 编辑设备
     */
    async emedietget() {
        let data = {
            ...this.data.fromdata,
            ...this.data.obj
        }
        console.log(data, '传输数据');
        wx.showLoading({
            mask: true
        })
        try {
            let res = await api.emedit({
                ...data
            })
            console.log(res, '编辑设备请求');
            if (res.code == 0) {
                wx.showToast({
                    title: '修改成功',
                    icon: 'success',
                    duration: 2000
                })
                wx.hideLoading()
                setTimeout(()=>{
                    wx.navigateBack(1)
                },500)
               
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error',
                    duration: 2000
                })
                wx.hideLoading()
            }

        } catch (error) {
            wx.hideLoading()
            wx.showToast({
                title: res.msg,
                icon: 'error',
                duration:2000
            })
        }

    },
    tijiaofagdu: util.throttle(function () {
        this.emedietget()
    },1000),
    /**
     * 设备详情
     */
    async getdevicedetail() {

        let res = await api.devicedetail({
            id: this.data.emid
        })
        console.log(res, '设备详情');

        if (res.code == 0) {
            this.setData({
                emdata: res.data,
                dpc: res.data.province + '' + res.data.city + '' + res.data.district,
                ['obj.name']: res.data.name,
                ['obj.address']: res.data.address,
                ['fromdata.device_id']: this.data.emid,
                ['fromdata.province']: res.data.province,
                ['fromdata.city']: res.data.city,
                ['fromdata.district']: res.data.district
            })
            // console.log(this.data.stocknum);
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getdevicedetail()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})